package br.com.fidalgotecnologia.layar.bean;

import java.math.BigDecimal;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.Marker;

import br.com.fidalgotecnologia.layar.CtrlLayar.CtrlLayar;
import br.com.fidalgotecnologia.layar.bean.abs.AbstractInserirHotSpot;
import br.com.fidalgotecnologia.layar.entity.Anchor;
import br.com.fidalgotecnologia.layar.entity.Axis;
import br.com.fidalgotecnologia.layar.entity.Geolocation;
import br.com.fidalgotecnologia.layar.entity.HotSpot;
import br.com.fidalgotecnologia.layar.entity.Icon;
import br.com.fidalgotecnologia.layar.entity.Layer;
import br.com.fidalgotecnologia.layar.entity.Rotation;
import br.com.fidalgotecnologia.layar.entity.Text;
import br.com.fidalgotecnologia.layar.entity.Transform;

@ManagedBean(name="InserirHotSpot")
@SessionScoped
public class InserirHotSpot extends AbstractInserirHotSpot{
	
	public void definirTipo(){
		if(getRadioTipo().equalsIgnoreCase("geolocalizado")){
			getOutPutPanelImagem().setRendered(false);
			getReferenciaImagem().setRendered(false);
			getMap().setRendered(true);
			getOutPutPanelGeolocation().setRendered(true);
		}else if(getRadioTipo().equalsIgnoreCase("imagem")){
			getOutPutPanelGeolocation().setRendered(false);
			getMap().setRendered(false);
			getOutPutPanelImagem().setRendered(true);
			getReferenciaImagem().setRendered(true);
		}
	}
	public void onPointSelect(PointSelectEvent event) {  
        LatLng latlng = event.getLatLng();  
        if(getMarker() == null){
        	setMarker(new Marker(latlng));
        } else{
        	getMarker().setLatlng(latlng);
        } 
        if(getModel() == null){
        	setModel(new DefaultMapModel());
        }
       getModel().addOverlay(getMarker());
       setCenter(Double.toString(getMarker().getLatlng().getLat())+","+Double.toString(getMarker().getLatlng().getLng()));
    }
	
	public InserirHotSpot(){
		setCenter("-15.799852,-47.862282");
		setHotSpot(new HotSpot());
		setLayer(new Layer());
	}
	
	public String cadastrar(){
		String retorno = null;
		Anchor anchor = new Anchor();
		if(getRadioTipo().equalsIgnoreCase("imagem")){
			anchor.setReferenceImage(getReferenciaImagem().getValue().toString());
		}else{
			Geolocation geo = new Geolocation();
			String [] center = getCenter().split(",");
			geo.setLat(new BigDecimal(center[0]));
			geo.setLon(new BigDecimal(center[1]));
			geo.setAlt(new BigDecimal(0d));
			anchor.setGeolocation(geo);
		}
		getHotSpot().setAnchor(anchor);
		getHotSpot().setBiwStyle(getBiwStyle());
		getHotSpot().setDoNotIndex(isIndexar());
		Icon icon = new Icon();
		if(getIcone()!=null)
			icon.setArquivo(getIcone());
		icon.setType(getTipoIcone());
		getHotSpot().setIcon(icon);
		if(getImagem()!=null)
			getHotSpot().setImagem(getImagem());
		getHotSpot().setInFocus(isInFocus());
		getHotSpot().setLayer(getLayer());
		br.com.fidalgotecnologia.layar.entity.Object object = new br.com.fidalgotecnologia.layar.entity.Object();
		if(getObjeto()!=null){
			object.setContentType(getTipoObjeto());
		    object.setFile(getObjeto());
		    object.setReducedFile(getObjetoReduzido());
		}
		object.setSize(getTamanho());
		getHotSpot().setObject(object);
		getHotSpot().setShowBiwOnClick(isShowBiwOnClick());
		getHotSpot().setShowSmallBiw(isShowBiwOnClick());
		Text text = new Text();
		text.setTitle(getTitulo());
		text.setFootnote(getRodape());
		text.setDescription(getDescricao());
		getHotSpot().setText(text);
		Transform transform = new Transform();
		Rotation rotate = new Rotation();
		rotate.setAngle(getAngulo());
		rotate.setRelative(isRelativo());
		Axis rotateAxis = new Axis();
		rotateAxis.setX(getRotateX());
		rotateAxis.setY(getRotateY());
		rotateAxis.setZ(getRotateZ());
		rotate.setAxis(rotateAxis);
		transform.setRotate(rotate);
		transform.setScale(getEscala());
		Axis translate = new Axis();
		translate.setX(getTranlateX());
		translate.setY(getTranlateY());
		translate.setZ(getTranlateZ());
		transform.setTranslate(translate);
		getHotSpot().setTransform(transform);
		if(CtrlLayar.getInstance().cadastrarHotSpot(getHotSpot())){
			retorno = "sucesso";
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("InserirHotSpot",new InserirHotSpot());  
		}else{
			retorno = "fail";
		}
		return retorno;
	}
	public void uploadImagem(FileUploadEvent event){
		byte[] aux = new byte[event.getFile().getContents().length];
		int i = 0;
		while(i<event.getFile().getContents().length){
			aux[i]=event.getFile().getContents()[i];
			i++;
		}
		setImagem(aux);
	}
	public void uploadIcone(FileUploadEvent event){
		byte[] aux = new byte[event.getFile().getContents().length];
		int i = 0;
		while(i<event.getFile().getContents().length){
			aux[i]=event.getFile().getContents()[i];
			i++;
		}
		setIcone(aux);
	}
	public void uploadObjeto(FileUploadEvent event){
		byte[] aux = new byte[event.getFile().getContents().length];
		int i = 0;
		while(i<event.getFile().getContents().length){
			aux[i]=event.getFile().getContents()[i];
			i++;
		}
		setObjeto(aux);
	}
	public void uploadObjetoReduzido(FileUploadEvent event){
		byte[] aux = new byte[event.getFile().getContents().length];
		int i = 0;
		while(i<event.getFile().getContents().length){
			aux[i]=event.getFile().getContents()[i];
			i++;
		}
		setObjetoReduzido(aux);
	}

}
